package com.minshenglife.zanzan.web.power;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.minshenglife.commons.dal.Page;
import com.minshenglife.zanzan.entity.User;
import com.minshenglife.zanzan.entity.power.ManagerEmp;
import com.minshenglife.zanzan.entity.power.RankPower;
import com.minshenglife.zanzan.entity.power.TempDept;
import com.minshenglife.zanzan.service.power.ManagerEmpService;
import com.minshenglife.zanzan.service.power.TempDeptService;
import com.minshenglife.zanzan.service.power.UserPowerService;
import com.minshenglife.zanzan.web.power.userpower.UserPowerPage;

/**
 * 人员权限管理
 * 
 * @author wangpeng
 */
@Controller
@RequestMapping("/userPower")
public class UserPowerController {
	Logger log = LoggerFactory.getLogger(UserPowerController.class);

	@Resource
	private UserPowerService userPowerService;

	@Resource
	ManagerEmpService managerEmpService;

	@Resource
	TempDeptService tempDeptService;

	/**
	 * 分页显示用户
	 * 
	 * @param pp
	 * @param modeMap
	 * @return
	 */
	@RequestMapping(value = "/loadUserPower.power", method = RequestMethod.GET)
	public String loadUserPower(HttpServletRequest request, UserPowerPage pp, ModelMap modleMap) {
		
		log.info("start    loadUserPower()");
		int pageNo = pp.getPageNo() == 0 ? 1 : pp.getPageNo();
		int pageSize = pp.getPageSize() == 0 ? 10 : pp.getPageSize();
		HttpSession session = request.getSession();
		ManagerEmp managerEmp = (ManagerEmp) session.getAttribute("managerEmp");
		if (managerEmp.getGrade().equals("1")) {
			modleMap.put("dept_name", "");
		} else {
			String dept_name = managerEmpService.finddet_name(managerEmp.getDept_id());
			modleMap.put("dept_name", dept_name);
		}
		Page<RankPower> page = userPowerService.findUserPage(modleMap, pageNo, pageSize);
		modleMap.addAttribute("page", page);
		return "/jsp/power/userPower";
	}

	/**
	 * 修改个人权限
	 * 
	 * @param map
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/updatePower.power", method = RequestMethod.GET)
	public String updateUserPower(ModelMap map, String empId) {
		log.info("start    updateUserPower()");
		User user = userPowerService.findUserId(empId);
		map.addAttribute("user", user);
		map.addAttribute("empId", user.getEmpId());
		map.addAttribute("empName", user.getEmp_name());
		map.addAttribute("postId", user.getPostId());
		return "/jsp/power/userPowerUpdate";
	}

	/**
	 * 根据用户名获取用户
	 * 
	 * @param map
	 * @param pp
	 * @param userName
	 * @return
	 */
	@RequestMapping(value = "/findUserName.power", method = RequestMethod.GET)
	public String findUserName(HttpServletRequest request, ModelMap modleMap, String userName) {
		log.info("start    findUserName()");
		HttpSession session = request.getSession();
		ManagerEmp managerEmp = (ManagerEmp) session.getAttribute("managerEmp");
		String dept_name = "";
		if (managerEmp.getGrade().equals("1")) {
		} else {
			dept_name = managerEmpService.finddet_name(managerEmp.getDept_id());
		}
		Page<RankPower> list = userPowerService.findUsesrName(userName,dept_name);
		modleMap.addAttribute("page", list);
		modleMap.addAttribute("userName", userName);
		return "/jsp/power/userPower";
	}

	/**
	 * 保存修改用户权限
	 * 
	 * @param empId
	 * @param postId
	 * @return
	 */
	@RequestMapping(value = "/saveUpdatePower.power", method = RequestMethod.GET)
	public String saveUpdatePower(HttpServletRequest request, ModelMap modelMap, String empId, String postId) {
		log.info("start    saveUpdatePower()");
		TempDept tempDept = tempDeptService.findTD(empId);
		if (tempDept != null && tempDept.getId() != null) {
			tempDept.setPostId(postId);
			tempDeptService.updateTempDeptPostId(tempDept);
		} else {
			tempDept = new TempDept();
			tempDept.setEmpId(empId);
			tempDept.setPostId(postId);
			tempDeptService.addTempDeptPostId(tempDept);
		}
		return "/jsp/power/results";
	}

}
